//
//  EMSSkillQueueFieldCell.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 4/10/09.
//  Copyright 2009  __Vadim__. All rights reserved.
//

#import "EMSSkillQueueFieldCell.h"
#import "EVEAPICoreDataController.h"
#import "EVECDQueueItem.h"
#import "EVECDLearnedSkill.h"
#import "EVECDSkill.h"

@implementation EMSSkillQueueFieldCell

@synthesize queueItem;

-(void)drawLevel
{
	NSString* currentLevelString = [NSString stringWithFormat:@"Level %@", self.queueItem.queuedLevel];
	NSSize currentLevelStringSize = [currentLevelString sizeWithAttributes:[self regularStringAttributesWithColor:[self stringColor]]];
	[self drawRegularString:currentLevelString atPoint:NSMakePoint(rightSide - currentLevelStringSize.width - 70, top)];
}

-(void)drawBars
{
	//Add a new draw layer
	[[NSGraphicsContext currentContext] saveGraphicsState];
	[[NSGraphicsContext currentContext] setShouldAntialias:NO];
	NSImageInterpolation interpolation = [[NSGraphicsContext currentContext] imageInterpolation];
	[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
	[[NSColor blackColor] set];
	
	//Draw Level Status Bar	
	[NSBezierPath strokeRect:NSMakeRect(rightSide - 66, top + 2, 62, 13)];
	
	//Draw filled in boxes for current level as well as gray boxes
	int i;
	for(i = 1; i <= 5; i++)
	{
		if(i > self.learnedSkill.level.intValue && i <= self.queueItem.queuedLevel.intValue)
		{
			[[NSColor blueColor] set];
		}
		if(i > self.queueItem.queuedLevel.intValue)
		{
			[[NSColor lightGrayColor] set];
		}
		[NSBezierPath fillRect:NSMakeRect(rightSide - 64 + ((i - 1) * 12), top + 3, 11, 10)];
	}
	[[NSColor blackColor] set];
	
	if(self.learnedSkill.level.intValue + 1 == self.queueItem.queuedLevel.intValue)
	{
		//Draw Percent Done Status Bar
		[NSBezierPath strokeRect:NSMakeRect(rightSide - 66, top + 18, 62, 9)];
		[[NSColor lightGrayColor] set];
		[NSBezierPath setDefaultLineWidth:5.0];
		[NSBezierPath strokeLineFromPoint:NSMakePoint(rightSide - 64, top + 22) 
								  toPoint:NSMakePoint(rightSide - 5 , top + 22)];
		if(percentCompleted)
		{
			[[NSColor blackColor] set];
			float percentBarEnd = 59 - (59 * percentCompleted) / 100;
			[NSBezierPath strokeLineFromPoint:NSMakePoint(rightSide - 64, top + 22) 
									  toPoint:NSMakePoint(rightSide - percentBarEnd - 5 , top + 22)];
		}
	}
	
	//Finalize new draw layer
	[[NSGraphicsContext currentContext] setImageInterpolation: interpolation];
	[[NSGraphicsContext currentContext] restoreGraphicsState];	
}

-(void)drawPercentCompleted
{
	if(self.learnedSkill.level.intValue + 1 == self.queueItem.queuedLevel.intValue)
	{
		[super drawPercentCompleted];
	}
}

-(void)drawPositionBar
{
	EMSRange range = [[(NSTableView*)[self controlView] delegate] queueStatusForCell:self];
	float barTop = bottom - 4;
	float postionBarStart = range.location * width;
	float end = range.location + range.length;
	float postionBarEnd = end * width;
	if(end > 1)
	{
		postionBarEnd = width;
	}
	float positionBarWidth = postionBarEnd - postionBarStart;
	float postionBarHeight = bottom - barTop;
	[[NSColor blueColor] set];
	[[NSGraphicsContext currentContext] saveGraphicsState];
	[[NSGraphicsContext currentContext] setShouldAntialias:NO];
	NSImageInterpolation interpolation = [[NSGraphicsContext currentContext] imageInterpolation];
	[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
	[NSBezierPath fillRect:NSMakeRect(postionBarStart, barTop, positionBarWidth, postionBarHeight)];
	[[NSGraphicsContext currentContext] setImageInterpolation: interpolation];
	[[NSGraphicsContext currentContext] restoreGraphicsState];	
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{	
	//set some defaults
	[NSBezierPath setDefaultLineWidth:1.0];
	[NSBezierPath setDefaultLineJoinStyle:NSBevelLineJoinStyle];
	leftSide = cellFrame.origin.x;
	rightSide = cellFrame.origin.x + cellFrame.size.width;
	top = cellFrame.origin.y;
	bottom = cellFrame.origin.y + cellFrame.size.height;
	width = cellFrame.size.width;
	NSManagedObjectID* data = [self objectValue];
	self.queueItem = (EVECDQueueItem*)[EVECDContext objectWithID:data];
	self.learnedSkill = queueItem.queuedSkill;
	self.learnedGroup = nil;
	level = learnedSkill.level.intValue;
	rank = learnedSkill.skill.rank.intValue;
	skillPointsLearned = learnedSkill.skillPointsLearned.intValue;
	percentCompleted = [EMSSkillTrainingInfo percentCompletedForCurrentPoints:skillPointsLearned
																			level:level
																			 rank:rank];
		
	[self drawName];
	[self drawRank];
	[self drawLevel];
	[self drawPercentCompleted];
	[self drawBars];	
	[[NSColor blueColor] set];
	
	//position = [[(NSTableView*)[self controlView] delegate] percentPositionForTrainingQueueCell:self];
	//NSLog(@"Position Start: %f End: %f, value: %@", position.start, position.end, self.objectValue);
	[self drawPositionBar];
}

@end
